<?php

namespace Home\Model;
use Think\Model;

/**
 * Description of CollectModel
 *
 * @author Dark
 */
class CollectModel extends Model {
    public function getAllCollect($userId){
        $sql="SELECT a.ID AS collectId,a.isBingo,b.goodsName,b.`unitPrice`,a.collectDate,c.totalNumber,c.luckyNumber
            FROM EachGo.`t_collect` a JOIN EachGo.`t_goods` b
            ON a.goods_ID = b.`ID` LEFT JOIN EachGo.`t_lottery_goods_info` c
            ON a.goods_ID=c.goods_ID
            WHERE a.`user_ID`=".$userId."
            ORDER BY a.collectDate DESC";
        return $this->query($sql);
    }
    public function delCollect($collectId){
        return $this->where("ID=".$collectId)->delete();
    }
    /*
     * 增加到收藏夹
     */
    public function addToCollect($data) {
        return $this->add($data);
    }
    
    /*
     * 删除收藏商品，只传userid是用户全部记录删除
     */
    public function deleteByID($data) {
        $sqlIn='';
        foreach($data as $k=>$v){
            $sqlIn=$sqlIn.$v.',';
        }
        $sqlIn = substr($sqlIn,0,strlen($sqlIn)-1);
        $sql="DELETE FROM EachGo.`t_collect`
              WHERE ID IN(".$sqlIn.") ";
        return $this->execute($sql);
    }
    /*
     * 查找拼购和购物收藏   bingo拼购，shopping购物
     */
    public function getCollectByType($type,$userID) {
        if( $type == 0) {
            if(!empty($userID)) {
                return $this->where('user_ID='.$userID.' and isBingo=0')->select();
            } else {
                Log::write('CollectModel->getCollectByType($type,$userID)传入userID为空');
                return FALSE;
            }
        } else if($type == 1) {
            if(!empty($userID)) {
                return $this->where('user_ID='.$userID.' and isBingo=1')->select();
            } else {
                Log::write('CollectModel->getCollectByType($type,$userID)传入userID为空');
                return FALSE;
            }
        }
    }
    
    /*
     * 时间段查询收藏夹
     */
    public function queryByTime($time,$userID) {
        switch($time) {
            case 1: 
                $sql = 'select * from EachGo.t_collect where user_ID='.$userID.' and collecDate>DATE_SUB(CURDATE(), INTERVAL 1 MONTH) order by collecDate desc';
                $results = $this->query($sql);
                break;
            case 3:
                $sql = 'select * from EachGo.t_collect where user_ID='.$userID.' and collecDate>DATE_SUB(CURDATE(), INTERVAL 3 MONTH) order by collecDate desc';
                $results = $this->query($sql);
                break;
            case 6:
                $sql = 'select * from EachGo.t_collect where user_ID='.$userID.' and collecDate>DATE_SUB(CURDATE(), INTERVAL 6 MONTH) order by collecDate desc';
                $results = $this->query($sql);
                break;
            case 12:
                    $sql = 'select * from EachGo.t_collect where user_ID='.$userID.' and collecDate>DATE_SUB(CURDATE(), INTERVAL 12 MONTH) order by collecDate desc';
                $results = $this->query($sql);
                break;
            default :
                $results = $this->where('user_ID='.$userID)->order('collectDate desc')->select();
                break;
        }
        return $results;
    }
}
